// pages/order/pay.js
const App = getApp()
import Kits from '../../utils/kits'
import Util from '../../utils/util'

Page({
  data: {
    init: false,
    loading: false,

    debug: App.Global.Debug,

    biz: '',

    id: '',
    total: '0',
    time: 0,
    timeData: {},

    pid: '',

    src: '',

    timer: undefined

  },
  onLoad(options) {
    console.log("into pay", options)
    let info = JSON.parse(options.info)
    console.log(info)
    this.setData({
      total: Kits.fmtMoneyString(info.total),
      id: info.id,
      time: info.expire * 1000,
      biz: options.biz,
      src: options.src,
      init: true
    })

  },
  onUnload() {
    if (this.data.timer) {
      clearTimeout(this.data.timer)
    }
  },
  onCountDownChange(e) {
    this.setData({
      timeData: e.detail,
    })
  },
  execTestPay() {
    if (!this.data.debug) {
      return
    }
    if (this.data.loading) {
      return
    }
    this.setData({
      loading: true
    })
    App.doPost({
      uri: '/app/order/action/pay',
      body: {
        type: this.data.biz,
        data: this.data.id,
        pcnl: 21,
        parg: JSON.stringify({
          appid: App.Global.AppId,
          puser: App.Global.OpenId
        })
      },
      fnSucc: resp => {
        console.log('data', resp.data)
        this.data.pid = resp.data.id

        this.execCheckPay()
        App.doPost({
          uri: '/dev/op/payment/finish',
          body: {
            pid: this.data.pid,
          },
          fnSucc: resp => {
              console.log('resp', resp);
          },
          fnFail: resp => {
            console.log('fail resp', resp);
          }
        })
      },
      fnFail: resp => {
        Util.Notify.error(`[${resp.stat.err}] ${resp.stat.msg}`)
      },
      fnComp: () => {
        this.setData({
          loading: false
        })
      }
    })
  },
  execPay() {
    if (this.data.loading) {
      return
    }
    this.setData({
      loading: true
    })
    App.doPost({
      uri: '/app/order/action/pay',
      body: {
        type: this.data.biz,
        data: this.data.id,
        pcnl: 21,
        parg: JSON.stringify({
          appid: App.Global.AppId,
          puser: App.Global.OpenId
        })
      },
      fnSucc: resp => {
        this.data.pid = resp.data.id
        console.log(resp.data)
        wx.requestPayment({
          timeStamp: resp.data.wxmn.timeStamp,
          nonceStr: resp.data.wxmn.nonceStr,
          package: resp.data.wxmn.package,
          signType: resp.data.wxmn.signType,
          paySign: resp.data.wxmn.paySign,
          success(res) {
            console.log(res)
            this.execCheckPay()
          },
          fail(res) {
            console.log(res)
            App.doPost({
              uri: '/app/base/payment/cancel',
              body: {
                id: resp.data.id,
              }
            })
          }
        })
      },
      fnFail: resp => {
        Util.Notify.error(`[${resp.stat.err}] ${resp.stat.msg}`)
      },
      fnComp: () => {
        this.setData({
          loading: false
        })
      }
    })
  },
  execCheckPay() {
    Util.Toast.loading('检查支付中...')
    App.doPost({
      uri: '/app/base/payment/state',
      body: {
        id: this.data.pid,
      },
      fnSucc: resp => {
        console.log('status', resp);
        if (resp.data.state == 1) {
          if (this.data.timer) {
            clearTimeout(this.data.timer)
          }
          this.data.timer = setTimeout(() => {
            this.execCheckPay()
          }, 1000)
          return
        }
        if (resp.data.state == 2) {
          wx.pub("ReloadPayState", {})
          if (this.data.src == 'detail') {
            wx.navigateBack({
              delta: 1,
            })
          } else {
            wx.redirectTo({
              url: `/pages/mine/order/detail?oid=${this.data.id}`,
            })
          }

        }
        Util.Toast.clear()

      },
    })
  }
})